
var getPixelRatio = function (context) {
  var backingStore = context.backingStorePixelRatio ||
    context.webkitBackingStorePixelRatio ||
    context.mozBackingStorePixelRatio ||
    context.msBackingStorePixelRatio ||
    context.oBackingStorePixelRatio ||
    context.backingStorePixelRatio || 1;
  return (window.devicePixelRatio || 1) / backingStore;
};

function downPdf(id, title) {
  var scale = 2
  var dom = document.getElementById(id)
  var width = dom.offsetWidth;
  var height = dom.offsetHeight;
  var title = title
  var c = document.createElement('canvas')

  c.width = width * scale
  c.height = height * scale
  c.style.width = width + "px";
  c.style.height = height + "px";
  c.getContext("2d").translate(-dom.offsetLeft * scale, -dom.offsetTop * scale);
  c.getContext("2d").scale(scale, scale);
  html2canvas(dom, {
    scale: scale,
    canvas: c,
    // logging: true,
    // width: width,
    // height: height,
    background: "#fff",
    onrendered: function (canvas) {
      var contentWidth = canvas.width
      var contentHeight = canvas.height
      var pageHeight = contentWidth / 592.28 * 841.89
      var leftHeight = contentHeight
      var position = 0
      var imgWidth = 595.28
      var imgHeight = 592.28 / contentWidth * contentHeight
      var pageData = canvas.toDataURL('image/jpeg', 1.0)
      var PDF = new jsPDF('', 'pt', 'a4')
      if (leftHeight < pageHeight) {
        PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
      } else {
        while (leftHeight > 0) {
          PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
          leftHeight -= pageHeight
          position -= 841.89
          if (leftHeight > 0) {
            PDF.addPage()
          }
        }
      }
      title = title || 'pdf'
      PDF.save(title + '.pdf')
    }
  })
}
